# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
import json

from tornado.options import options
from tornado.web import RequestHandler

tornado.options.define("port", default=8000, type=int, help="run server on the given port")


class IndexHandler(RequestHandler):
    """请求处理类"""
    def set_default_headers(self):
        self.set_header("Content-Type", "application/json;")
        self.set_header("Itcast", "python")

    def get(self):
        stu = {
            "name": "zhangsan",
            "age": 23
        }
        # self.write(stu)
        json_data = json.dumps(stu)
        self.write(json_data)
        self.set_status(250, reason="Itcast")

    def post(self):
        stu = {
            "name": "zhangsan",
            "age": 23
        }
        json_data = json.dumps(stu)
        self.write(json_data)
        self.set_header("Itcast", "C")


if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        [(r"/", IndexHandler)],
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()